package com.yl.framework.context.support;

import com.yl.framework.beans.factory.support.BeanDefinitionReader;
import com.yl.framework.beans.factory.support.BeanDefinitionRegistry;
import com.yl.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 小羊
 * @Date 2/11/2024下午 3:52
 * @ClassName AbstractApplicationContext
 * @Description ApplicationContext接口的子实现类，用于非延时加载
 */
public abstract class AbstractApplicationContext implements ApplicationContext {
    /**
     * 声明解析器变量
     */
    protected BeanDefinitionReader beanDefinitionReader;
    /**
     *定义用于存储bean对象的map容器
     */
    protected Map<String,Object> singletonObjects = new HashMap<>();

    /**
     * 声明配置文件路径的变量
     */
    protected String configLocation;
    @Override
    public void refresh() throws Exception {
        //加载beanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //初始化bean
        finishBeanInitialization();
    }

    /**
     * bean的初始化
     * @throws Exception
     */
    private void finishBeanInitialization() throws Exception{
        //获取注册表的对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取beanDefinition对象
        String[] names = registry.getBeanDefinitionNames();
        //进行bean初始化
        for (String name : names) {
            getBean(name);
        }
    }
}
